package com.wlzg.collectiontool.view

import android.content.Context
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import androidx.lifecycle.Lifecycle
import com.wlzg.collectiontool.R
import kotlinx.android.synthetic.main.activity_mcollection.*
import android.util.DisplayMetrics

import android.view.WindowManager
import androidx.lifecycle.lifecycleScope
import com.wlzg.collectiontool.util.MConfigUtil
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext


class MCollectionActivity : BaseActivity() {
    val fragmentList = mutableListOf<Fragment?>(ManageFragment(),
        InvestMapFragment(),AnalyseFragment(),UserFragment())
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_mcollection)
        MConfigUtil.initLogindata()
        initTab()
        getAndroiodScreenProperty()
    }
    fun getAndroiodScreenProperty() {
        val wm = this.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        val dm = DisplayMetrics()
        wm.defaultDisplay.getMetrics(dm)
        val width = dm.widthPixels // 屏幕宽度（像素）
        val height = dm.heightPixels // 屏幕高度（像素）
        val density = dm.density // 屏幕密度（0.75 / 1.0 / 1.5）
        val densityDpi = dm.densityDpi // 屏幕密度dpi（120 / 160 / 240）
        // 屏幕宽度算法:屏幕宽度（像素）/屏幕密度
        val screenWidth = (width / density).toInt() // 屏幕宽度(dp)
        val screenHeight = (height / density).toInt() // 屏幕高度(dp)
        Log.d("h_bl", "屏幕宽度（像素）：$width")
        Log.d("h_bl", "屏幕高度（像素）：$height")
        Log.d("h_bl", "屏幕密度（0.75 / 1.0 / 1.5）：$density")
        Log.d("h_bl", "屏幕密度dpi（120 / 160 / 240）：$densityDpi")
        Log.d("h_bl", "屏幕宽度（dp）：$screenWidth")
        Log.d("h_bl", "屏幕高度（dp）：$screenHeight")
    }
    var toIndex=0
    var fromIndex=0
    var first=false
    var isResmue=false
    var special=false

    private fun initTab() {
        tab_layout?.apply {
            configTabLayoutConfig {
                onSelectIndexChange = { fromIndex, selectIndexList, reselect, fromUser ->
                    if (!reselect) {
                        toIndex = selectIndexList.first()
                        this@MCollectionActivity.fromIndex=fromIndex

                        show(fragmentList[toIndex]!!, fragmentList.getOrNull(fromIndex))}
                    if(isResmue&&toIndex==0||toIndex==1){
//                        showandHide(true)
                    }
                }
            }
        }
    }


    fun showandHide(special:Boolean){
isResmue=false
//        if(!first&&toIndex==0&&fromIndex==0){
        if(!special){
        if(!first&&toIndex==0){
            Log.d("hghsjk","kef"+toIndex+"keh"+fromIndex)
            show(fragmentList[1]!!, fragmentList.getOrNull(0))
            show(fragmentList[0]!!, fragmentList.getOrNull(1))
            lifecycleScope.launch { withContext(Dispatchers.IO){
                delay(150)
            }
                withContext(Dispatchers.Main){
                    show(fragmentList[1]!!, fragmentList.getOrNull(0))
                    show(fragmentList[0]!!, fragmentList.getOrNull(1))
                }
            }
        }
//        if(!first&&toIndex==1&&fromIndex==0){
        if(!first&&toIndex==1){
            Log.d("hghsjk","ssssss"+toIndex+"sss"+fromIndex)
//            show(fragmentList[0]!!, fragmentList.getOrNull(1))
//            show(fragmentList[1]!!, fragmentList.getOrNull(0))
            lifecycleScope.launch { withContext(Dispatchers.IO){
                delay(100)
            }
                withContext(Dispatchers.Main){
                    show(fragmentList[0]!!, fragmentList.getOrNull(1))
                    withContext(Dispatchers.IO){
                        delay(150)
                    }

                    show(fragmentList[1]!!, fragmentList.getOrNull(0))
                }
            }
        }
        }else{
            if(!first&&toIndex==1){
                Log.d("hghsjk","ssssss"+toIndex+"sss"+fromIndex)
//            show(fragmentList[0]!!, fragmentList.getOrNull(1))
//            show(fragmentList[1]!!, fragmentList.getOrNull(0))
                lifecycleScope.launch { withContext(Dispatchers.IO){
                }
                    withContext(Dispatchers.Main){
                        show(fragmentList[0]!!, fragmentList.getOrNull(1))
                        withContext(Dispatchers.IO){
                            delay(20)
                        }

                        show(fragmentList[1]!!, fragmentList.getOrNull(0))
                    }
                }
            }
        }
    }



    override fun onResume() {
        super.onResume()
        isResmue=true
Log.d("hghsjk","kessf"+toIndex)
//showandHide(false)


    }




    fun show(f: Fragment, hide: Fragment? = null) {
        supportFragmentManager.beginTransaction().apply {
            Log.d("testst", "ss${f.isAdded} ")
            if (f.isAdded) {
                show(f)
                setMaxLifecycle(f, Lifecycle.State.RESUMED)
            } else {
                add(R.id.frame_container_layout, f)
            }
            hide?.let {
                if (it.isAdded) {
                    hide(it)
                    setMaxLifecycle(it, Lifecycle.State.STARTED)
                }
            }
            commitAllowingStateLoss()
        }
    }
}